<?php

class Admin_Models_Product extends Libs_Model {

    private $pro_id;
    private $name;
    private $thumb;
    private $cat_id;
    private $description;
    private $price;
    private $status;
    private $post_date;

    public function setCatId($cat_id) {
        $this->cat_id = $cat_id;
    }

    public function getCatId() {
        return $this->cat_id;
    }

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setPostDate($post_date) {
        $this->post_date = $post_date;
    }

    public function getPostDate() {
        return $this->post_date;
    }

    public function setPrice($price) {
        $this->price = $price;
    }

    public function getPrice() {
        return $this->price;
    }

    public function setProId($pro_id) {
        $this->pro_id = $pro_id;
    }

    public function getProId() {
        return $this->pro_id;
    }

    public function setStatus($status) {
        $this->status = $status;
    }

    public function getStatus() {
        return $this->status;
    }

    public function setThumb($thumb) {
        $this->thumb = $thumb;
    }

    public function getThumb() {
        return $this->thumb;
    }

    public function setValue($row, $isKey = TRUE) {
        $pro = new Admin_Models_Product();
        if ($isKey) {
            $pro->setProId($row['pro_id']);
        }
        $pro->setCatId($row['cat_id']);
        $pro->setDescription($row['description']);
        $pro->setName($row['name']);
        $pro->setPostDate($row['post_date']);
        $pro->setPrice($row['price']);
        $pro->setStatus($row['status']);
        $pro->setThumb($row['thumb']);
        return $pro;
    }

    public function getKeyAndValue(Admin_Models_Product $pro, $isKey = FALSE) {
        $data = array();
        if ($isKey) {
            $data['pro_id'] = $pro->getProId();
        }
        $data['name'] = $pro->getName();
        $data['thumb'] = $pro->getThumb();
        $data['post_date'] = $pro->getPostDate();
        $data['price'] = $pro->getPrice();
        $data['status'] = $pro->getStatus();
        $data['description'] = $pro->getDescription();
        $data['cat_id'] = $pro->getCatId();
        return $data;
    }

    public function getAllProduct($limit = null) {
        if (isset($limit) && $limit != FALSE) {
            $sql = "select * from tbl_products order by post_date desc limit $limit";
        } else {
            $sql = "select * from tbl_products order by post_date desc";
        }
        $result = $this->db->fetchAllO($sql);
        while ($rows = mysql_fetch_assoc($result)) {
            $out[] = $this->setValue($rows);
        }
        return $out;
    }

    public function delete($pro_id) {
        if (isset($pro_id)) {

            $sql = "delete from tbl_products where pro_id = $pro_id";
            $this->db->query($sql);

            $sql = "select url from tbl_images where img_id = $pro_id";
            $images = $this->db->fetchAll($sql);

            $sql = "delete from tbl_images where pro_id = $pro_id";
            $this->db->query($sql);

            return $images;
        }
    }

    public function getProById($pro_id) {
        $sql = "select * from tbl_products where pro_id = $pro_id";
        $result = $this->db->fetchOneO($sql);
        if ($row = mysql_fetch_assoc($result)) {
            $out = $this->setValue($row);
        }

        return $out;
    }

    public function edit(Admin_Models_Product $pro, $pro_id) {
        $this->db->prepareUpdate($this->getKeyAndValue($pro));
        $this->db->updatePro('tbl_products', $pro_id);
    }

    public function insert(Admin_Models_Product $pro) {
        $this->db->prepareInsert($this->getKeyAndValue($pro));
        $this->db->insert('tbl_products');
        return $this->db->lastId();
    }
}
